﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cricket.LinqModel;
using System.ComponentModel;
using System.Data.Linq.Mapping;

namespace Cricket.LinqData
{
    public partial class DataRepository : IDataRepository
    {
        protected CricketDbContext _dbCricket;
        public DataRepository()
        {
            _dbCricket = CricketDatabaseFactory.CreateDbContext();
        }
        private void PopulateModel(object sourceObj, object targetObj)
        {
            var sourceProperties = TypeDescriptor.GetProperties(sourceObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);

            var targetProperties = TypeDescriptor.GetProperties(targetObj).Cast<PropertyDescriptor>()
                .Where(p => p.Attributes.OfType<ColumnAttribute>().Count() > 0);


            for (int i = 0; i < sourceProperties.Count(); i++)
            {
                targetProperties.ElementAt(i).SetValue(targetObj,
                    sourceProperties.ElementAt(i).GetValue(sourceObj));
            }

        }

        #region Admin
        public void CreateAdmin(Admin admin)
        {
            _dbCricket.Admins.InsertOnSubmit(admin);
            _dbCricket.SubmitChanges();
        }
        public void UpdateAdmin(Admin admin)
        {
            Admin updateObj = (from a in _dbCricket.Admins
                               where a.Id.Equals(admin.Id)
                               select a).FirstOrDefault();
            PopulateModel(admin, updateObj);
            _dbCricket.SubmitChanges();
        }
        public void UpdateAdminFlag(Admin admin)
        {
            Admin updateObj = (from a in _dbCricket.Admins
                               where a.Id.Equals(admin.Id)
                               select a).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsFirstLogin = false;
            }
            _dbCricket.SubmitChanges();
        }
        public void DeleteAdmin(Admin admin)
        {
            Admin deleteObj = (from a in _dbCricket.Admins
                               where a.Id.Equals(admin.Id)
                               select a).FirstOrDefault();
            if (deleteObj != null)
            {
                deleteObj.IsActive = false;
                deleteObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        public List<Admin> GetAllAdmin()
        {
            var result = (from a in _dbCricket.Admins
                          where a.IsActive.Equals(true) && a.IsDeleted.Equals(false) && a.UserType != 1
                          orderby a.FirstName
                          select a);
            return result.ToList();
        }
        public List<Admin> GetInActiveAdmin()
        {
            var result = (from a in _dbCricket.Admins
                          where a.IsActive.Equals(false) && a.IsDeleted.Equals(true) && a.UserType != 1
                          orderby a.FirstName
                          select a);
            return result.ToList();
        }
        public List<AdminResult> GetAdminDetailsById(int Id)
        {
            List<AdminResult> adminResult = new List<AdminResult>();
            var aResult = from a in _dbCricket.Admins
                          join ac in _dbCricket.AdminCredentials on a.Id equals ac.AdminId
                          where a.Id.Equals(Id)
                          select new AdminResult
                          {
                              FirstName = a.FirstName,
                              LastName = a.LastName,
                              Phone = a.Phone,
                              Email = a.Email,
                              City = a.City,
                              State = a.State,
                              Zip = a.Zip,
                              IsActive = Convert.ToBoolean(a.IsActive),
                              AdminCredentialId = ac.Id,
                              AdminId = Convert.ToInt32(ac.AdminId),
                              BatId = Convert.ToInt32(ac.BatId),
                              Username = ac.Username,
                              Password1 = ac.Password1,
                              Password2 = ac.Password2,
                              CommonRate = Convert.ToInt32(a.CommonRate),

                          };
            adminResult = aResult.ToList();
            return adminResult;
        }
        public List<SpLoginUserResult> GetAdminByCredential(string username, string password, string password2)
        {
            return _dbCricket.SpLoginUser(username, password, password2).ToList();
        }
        public List<GetCommonRateResult> GetCommonRate(int adminId)
        {
            return _dbCricket.GetCommonRate(adminId).ToList();
        }
        #endregion

        #region Admin Credential
        public void CreateAdminCredential(AdminCredential adminCredentail)
        {
            _dbCricket.AdminCredentials.InsertOnSubmit(adminCredentail);
            _dbCricket.SubmitChanges();
        }
        public void DeleteAdminCredentail(AdminCredential adminCredentail)
        {
            var deleteObj = (from ac in _dbCricket.AdminCredentials where ac.AdminId.Equals(adminCredentail.AdminId) select ac);
            foreach (var a in deleteObj)
            {
                _dbCricket.AdminCredentials.DeleteOnSubmit(a);
            }
            _dbCricket.SubmitChanges();
        }
        public void UpdateAdminCredentials(AdminCredential adminCredential)
        {
            AdminCredential updateObj = (from ac in _dbCricket.AdminCredentials
                                         where ac.AdminId.Equals(adminCredential.AdminId) && ac.BatId.Equals(adminCredential.BatId)
                                         select ac).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.Password1 = adminCredential.Password1;
                updateObj.Password2 = adminCredential.Password2;
            }
            _dbCricket.SubmitChanges();
        }

        public void UpdateAdminCredentialsMyProfile(AdminCredential adminCredential)
        {
            AdminCredential updateObj = (from ac in _dbCricket.AdminCredentials
                                         where ac.AdminId.Equals(adminCredential.AdminId) && ac.Id.Equals(adminCredential.Id)
                                         select ac).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.Password1 = adminCredential.Password1;
                updateObj.Password2 = adminCredential.Password2;
            }
            _dbCricket.SubmitChanges();
        }
        #endregion

        #region Client
        public void CreateClient(Client client)
        {
            _dbCricket.Clients.InsertOnSubmit(client);
            _dbCricket.SubmitChanges();
        }
        public List<GetAllClientsResult> GetAllClient()
        {
            return _dbCricket.GetAllClients().ToList();
        }
        public List<GetAllInActiveClientsResult> GetAllInActiveClient()
        {
            return _dbCricket.GetAllInActiveClients().ToList();
        }
        public void UpdateClient(Client client)
        {
            Client updateObj = (from c in _dbCricket.Clients
                                where c.Id.Equals(client.Id)
                                select c).FirstOrDefault();
            PopulateModel(client, updateObj);
            _dbCricket.SubmitChanges();
        }
        public void DeleteClient(Client client)
        {
            Client deleteObj = (from c in _dbCricket.Clients
                                where c.Id.Equals(client.Id)
                                select c).FirstOrDefault();
            if (deleteObj != null)
            {
                deleteObj.IsActive = false;
                deleteObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        public void GetClientById(Client client)
        {
            Client result = (from c in _dbCricket.Clients
                             where c.Id.Equals(client.Id)
                             select c).FirstOrDefault();

            if (result != null)
                PopulateModel(result, client);
        }
        public decimal? GetClientBettingLimit(int clientId)
        {
            return (from c in _dbCricket.Clients
                    where c.Id.Equals(clientId)
                    select c.Limit).SingleOrDefault();
        }
        public decimal? GetMatchClientBettingLimit(int clientId)
        {
            return (from c in _dbCricket.Clients
                    where c.Id.Equals(clientId)
                    select c.MatchTempLimit).SingleOrDefault();
        }
        public void UpdateClientLimit(int clientId, string limit)
        {
            Client updateObj = (from c in _dbCricket.Clients
                                where c.Id.Equals(clientId)
                                select c).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.Limit = Convert.ToInt32(updateObj.Limit) - Convert.ToInt32(limit);
            }
            _dbCricket.SubmitChanges();
        }
        public void UpdateClientMatchLimit(int matchScheduleId, int clientId)
        {
            _dbCricket.GetMatchDeductionLimit(matchScheduleId, clientId);
        }
        public List<GetMinAmountResult> GetMinAmount(int clientId, int matchScheduleId)
        {
            return _dbCricket.GetMinAmount(clientId, matchScheduleId).ToList();
        }
        public List<GetTotalOfSessionResult> GetTotalOfSession(int clientId, int matchScheduleId)
        {
            return _dbCricket.GetTotalOfSession(clientId, matchScheduleId).ToList();
        }
        public List<GetClientsByAdminIdResult> GetClientsByAdminId(int AdminId)
        {
            return _dbCricket.GetClientsByAdminId(AdminId).ToList();
        }
        public List<GetInActiveClientsByAdminIdResult> GetInActiveClientsByAdminId(int AdminId)
        {
            return _dbCricket.GetInActiveClientsByAdminId(AdminId).ToList();
        }
        public List<Client> CheckUserNameExist(int ClientId, string username)
        {
            var result = (from c in _dbCricket.Clients
                          where c.Username.ToLower().Trim().Equals(username)
                          && c.Id != ClientId
                          select c);
            return result.ToList();
        }
        public List<ClientLoginResult> ClientLogin(string username, string password)
        {
            return _dbCricket.ClientLogin(username, password).ToList();
        }
        public List<GetTopClientForSuperAdminResult> GetTopClientForSuperAdmin()
        {
            return _dbCricket.GetTopClientForSuperAdmin().ToList();
        }
        public List<GetTopClientForAdminResult> GetTopClientForAdmin(int AdminId)
        {
            return _dbCricket.GetTopClientForAdmin(AdminId).ToList();
        }
        public void UpdateClientLimitFromMatch(int ClientId)
        {
            _dbCricket.UpdateClientLimitForMatch(ClientId);
        }
        public void UpdateClientLimitForSession(int ClientId)
        {
            _dbCricket.UpdateClientLimitForSession(ClientId);
        }
        public void UpdateClientLimitFromNineEleven(int ClientId)
        {
            _dbCricket.UpdateClientLimitFromNineEleven(ClientId);
        }
        public List<GetMinAmountForMatchResult> GetMinAmountForMatch(int MatchId, int ClientId)
        {
            return _dbCricket.GetMinAmountForMatch(MatchId, ClientId).ToList();
        }
        public List<GetTotalAmountForSessionResult> GetTotalAmountForSession(int MatchId, int ClientId)
        {
            return _dbCricket.GetTotalAmountForSession(MatchId, ClientId).ToList();
        }
        
        public List<GetFancy90110AmountResult> GetFancy90110Amount(int MatchId, int ClientId)
        {
            return _dbCricket.GetFancy90110Amount(MatchId, ClientId).ToList();
        }
        public void UpdateClientLimitByAdmin(Client client)
        {
            Client updateObj = (from c in _dbCricket.Clients
                                where c.Id.Equals(client.Id)
                                select c).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.Limit = client.ActualLimit;
                updateObj.ActualLimit = client.ActualLimit;
                updateObj.MatchTempLimit = client.ActualLimit;
                updateObj.NELimit = client.ActualLimit; 
            }
            _dbCricket.SubmitChanges();
        }

        public void UpdateClientLimitByClient(int clientId, decimal? limit)
        {
            Client updateObj = (from c in _dbCricket.Clients
                                where c.Id.Equals(clientId)
                                select c).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.Limit = limit;
                updateObj.ActualLimit = limit;
                updateObj.MatchTempLimit = limit;
                updateObj.NELimit = limit;
            }
            _dbCricket.SubmitChanges();
        }
        public void UpdateIsReloadForClient(Client client)
        {
            Client updateObj = (from c in _dbCricket.Clients
                                where c.Id.Equals(client.Id)
                                select c).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsReload = true;
            }
            _dbCricket.SubmitChanges();
        }

        public void UpdateIsLoginForClient(Client client)
        {
            Client updateObj = (from c in _dbCricket.Clients
                                where c.Id.Equals(client.Id)
                                select c).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsLogin = client.IsLogin;
            }
            _dbCricket.SubmitChanges();
        }
        public List<GetAllLoginClientsResult> GetAllLoginClient()
        {
            return _dbCricket.GetAllLoginClients().ToList();
        }

        public List<GetAllReloadClientsResult> GetAllReloadClients()
        {
            return _dbCricket.GetAllReloadClients().ToList();
        }

        public List<GetAllLoginByAdminResult> GetAllLoginByAdmin(int AdminId)
        {
            return _dbCricket.GetAllLoginByAdmin(AdminId).ToList();
        }


        public void LogoutAllClient()
        {
            _dbCricket.LogoutAllClient();
        }

        public void ReloadAllClient()
        {
            _dbCricket.ReloadAllClient();
        }
        

        #endregion

        #region Team
        public void CreateTeam(Team team)
        {
            _dbCricket.Teams.InsertOnSubmit(team);
            _dbCricket.SubmitChanges();
        }
        public List<Team> GetAllTeam()
        {
            var result = (from t in _dbCricket.Teams
                          where t.IsActive.Equals(true) && t.IsDeleted.Equals(false)
                          select t).ToList();
            return result;
        }

        public List<GetAllTeamsResult> GetAllTeams()
        {
            return _dbCricket.GetAllTeams().ToList();
        }

        public void DeleteTeam(Team team)
        {
            Team deleteObj = (from t in _dbCricket.Teams
                              where t.Id.Equals(team.Id)
                              select t).FirstOrDefault();
            if (deleteObj != null)
            {
                deleteObj.IsActive = false;
                deleteObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        public void GetTeamById(Team team)
        {
            Team result = (from t in _dbCricket.Teams
                           where t.Id.Equals(team.Id)
                           select t).FirstOrDefault();

            if (result != null)
                PopulateModel(result, team);
        }
        public void UpdateTeam(Team team)
        {
            Team updateObj = (from t in _dbCricket.Teams
                              where t.Id.Equals(team.Id)
                              select t).FirstOrDefault();
            PopulateModel(team, updateObj);
            _dbCricket.SubmitChanges();
        }
        public List<Team> CheckTeamNameExist(int TeamId, string teamName)
        {
            var result = (from t in _dbCricket.Teams
                          where t.TeamName.ToLower().Trim().Equals(teamName)
                          && t.Id != TeamId
                          select t);
            return result.ToList();
        }
        public List<Team> CheckAbbrivationExist(int TeamId, string abbrivation)
        {
            var result = (from t in _dbCricket.Teams
                          where t.Abbrivation.ToLower().Trim().Equals(abbrivation)
                          && t.Id != TeamId
                          select t);
            return result.ToList();
        }
        public List<Team> GetTeamsBySeriesId(int SeriesId)
        {
            var result = (from t in _dbCricket.Teams
                          where t.SeriesId.Equals(SeriesId) && t.IsActive.Equals(1) && t.IsDeleted.Equals(0)
                          orderby t.TeamName ascending
                          select t).ToList();
            return result;
        }
        #endregion

        #region Series
        public void CreateSeries(Series series)
        {
            _dbCricket.Series.InsertOnSubmit(series);
            _dbCricket.SubmitChanges();
        }
        public List<Series> GetAllSeries()
        {
            var result = (from t in _dbCricket.Series
                          where t.IsActive.Equals(true) && t.IsDeleted.Equals(false)
                          orderby t.Name ascending
                          select t).ToList();
            return result;
        }
        public void DeleteSeries(Series series)
        {
            Series deleteObj = (from t in _dbCricket.Series
                                where t.Id.Equals(series.Id)
                                select t).FirstOrDefault();
            if (deleteObj != null)
            {
                deleteObj.IsActive = false;
                deleteObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        public void GetSeriesById(Series series)
        {
            Series result = (from t in _dbCricket.Series
                             where t.Id.Equals(series.Id)
                             select t).FirstOrDefault();

            if (result != null)
                PopulateModel(result, series);
        }
        public void UpdateSeries(Series series)
        {
            Series updateObj = (from t in _dbCricket.Series
                                where t.Id.Equals(series.Id)
                                select t).FirstOrDefault();
            PopulateModel(series, updateObj);
            _dbCricket.SubmitChanges();
        }
        public List<Series> CheckSeriesNameExist(int SeriesId, string teamName)
        {
            var result = (from t in _dbCricket.Series
                          where t.Name.ToLower().Trim().Equals(teamName)
                          && t.Id != SeriesId
                          select t);
            return result.ToList();
        }
        public List<Series> CheckSeriesAbbrivationExist(int TeamId, string abbrivation)
        {
            var result = (from t in _dbCricket.Series
                          where t.Abbrivation.ToLower().Trim().Equals(abbrivation)
                          && t.Id != TeamId
                          select t);
            return result.ToList();
        }

        #endregion

        #region Match Schedule
        public void CreateMatchSchedule(MatchSchedule matchSchedule)
        {
            _dbCricket.MatchSchedules.InsertOnSubmit(matchSchedule);
            _dbCricket.SubmitChanges();
        }
        public List<GetAllMatchesResult> GetAllMatches()
        {
            return _dbCricket.GetAllMatches().ToList();
        }
        public void DeleteMatche(MatchSchedule matchSchedule)
        {
            MatchSchedule deleteObj = (from ms in _dbCricket.MatchSchedules
                                       where ms.Id.Equals(matchSchedule.Id)
                                       select ms).FirstOrDefault();
            if (deleteObj != null)
            {
                deleteObj.IsActive = false;
                deleteObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        public void GetMatchesById(MatchSchedule matchSchedule)
        {
            MatchSchedule result = (from ms in _dbCricket.MatchSchedules
                                    where ms.Id.Equals(matchSchedule.Id)
                                    select ms).FirstOrDefault();

            if (result != null)
                PopulateModel(result, matchSchedule);
        }
        public void UpdateMatchSchedule(MatchSchedule matchSchedule)
        {
            MatchSchedule updateObj = (from ms in _dbCricket.MatchSchedules
                                       where ms.Id.Equals(matchSchedule.Id)
                                       select ms).FirstOrDefault();
            PopulateModel(matchSchedule, updateObj);
            _dbCricket.SubmitChanges();
        }
        public List<MatchSchedule> CheckMatchExist(int matchId, int team1, int team2, DateTime date)
        {
            var result = (from ms in _dbCricket.MatchSchedules
                          where ms.Team1.Equals(team1) && ms.Team2.Equals(team2) && ms.Date.Equals(date)
                          && ms.Id != matchId
                          select ms);
            return result.ToList();
        }
        public List<GetTodayMatcheResult> GetTodayMatches(int matchType)
        {
            return _dbCricket.GetTodayMatche(matchType).ToList();
        }
        public List<GetMatchDetailsResult> GetMatchDetails(int matchId)
        {
            return _dbCricket.GetMatchDetails(matchId).ToList();
        }
        public List<GetTodayMatcheForClientResult> GetTodayMatchesForClient()
        {
            return _dbCricket.GetTodayMatcheForClient().ToList();
        }
        public List<GetTeamsByMatchResult> GetTeamsByMatch(int MatchId)
        {
            return _dbCricket.GetTeamsByMatch(MatchId).ToList();
        }
        public List<GetMatchTypeByMatchIdResult> GetMatchTypeByMatchId(int MatchId)
        {
            return _dbCricket.GetMatchTypeByMatchId(MatchId).ToList();
        }
        public List<GetUpComingMatchesResult> GetUpComingMatches()
        {
            return _dbCricket.GetUpComingMatches().ToList();
        }

        public List<GetMatchesForMatchResultResult> GetMatchesForMatchResult()
        {
            return _dbCricket.GetMatchesForMatchResult().ToList();
        }
        public List<GetMatchesForReportResult> GetMatchesForReport()
        {
            return _dbCricket.GetMatchesForReport().ToList();
        }
        public List<GetTeamNameByMatchDetailResult> GetTeamNameByMatchDetail(int MatchId)
        {
            return _dbCricket.GetTeamNameByMatchDetail(MatchId).ToList();
        }
        public List<GetAllMatchesForDeleteResult> GetAllMatchesForDelete()
        {
            return _dbCricket.GetAllMatchesForDelete().ToList();
        }
        public void DeleteData(int MatchId)
        {
            _dbCricket.DeleteData(MatchId);
        }
        #endregion

        #region Betting Type
        public void CreateBettingType(BettingType bettingType)
        {
            _dbCricket.BettingTypes.InsertOnSubmit(bettingType);
            _dbCricket.SubmitChanges();
        }
        public List<BettingType> GetAllBettingType()
        {
            var result = (from bt in _dbCricket.BettingTypes
                          where bt.IsActive.Equals(true) && bt.IsDeleted.Equals(false)
                          select bt).ToList();
            return result;
        }
        public void DeleteBettingType(BettingType bettingType)
        {
            BettingType deleteObj = (from bt in _dbCricket.BettingTypes
                                     where bt.Id.Equals(bettingType.Id)
                                     select bt).FirstOrDefault();
            if (deleteObj != null)
            {
                deleteObj.IsActive = false;
                deleteObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        public void GetBettingTypeById(BettingType bettingType)
        {
            BettingType result = (from bt in _dbCricket.BettingTypes
                                  where bt.Id.Equals(bettingType.Id)
                                  select bt).FirstOrDefault();

            if (result != null)
                PopulateModel(result, bettingType);
        }
        public void UpdateBettingType(BettingType bettingType)
        {
            BettingType updateObj = (from bt in _dbCricket.BettingTypes
                                     where bt.Id.Equals(bettingType.Id)
                                     select bt).FirstOrDefault();
            PopulateModel(bettingType, updateObj);
            _dbCricket.SubmitChanges();
        }
        #endregion

        #region Match Type
        public void CreateMatchType(MatchType matchType)
        {
            _dbCricket.MatchTypes.InsertOnSubmit(matchType);
            _dbCricket.SubmitChanges();
        }
        public List<MatchType> GetAllMatchType()
        {
            var result = (from mt in _dbCricket.MatchTypes
                          where mt.IsActive.Equals(true) && mt.IsDeleted.Equals(false)
                          select mt).ToList();
            return result;
        }
        public void DeleteMatchType(MatchType matchType)
        {
            MatchType deleteObj = (from mt in _dbCricket.MatchTypes
                                   where mt.Id.Equals(matchType.Id)
                                   select mt).FirstOrDefault();
            if (deleteObj != null)
            {
                deleteObj.IsActive = false;
                deleteObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        public void GetMatchTypeById(MatchType matchType)
        {
            MatchType result = (from mt in _dbCricket.MatchTypes
                                where mt.Id.Equals(matchType.Id)
                                select mt).FirstOrDefault();

            if (result != null)
                PopulateModel(result, matchType);
        }
        public void UpdateMatchType(MatchType matchType)
        {
            MatchType updateObj = (from mt in _dbCricket.MatchTypes
                                   where mt.Id.Equals(matchType.Id)
                                   select mt).FirstOrDefault();
            PopulateModel(matchType, updateObj);
            _dbCricket.SubmitChanges();
        }
        #endregion

        #region Betting
        public void CreateBetting(Betting betting)
        {
            _dbCricket.Bettings.InsertOnSubmit(betting);
            _dbCricket.SubmitChanges();
        }
        public void UpdateBetting(Betting betting)
        {
            Betting updateObj = (from b in _dbCricket.Bettings
                                 where b.BettingTypeId.Equals(betting.BettingTypeId) && b.AdminId.Equals(betting.AdminId)
                                 && b.IsDeleted == false
                                 select b).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsDeleted = true;
                updateObj.UpdatedDate = System.DateTime.Now;
            }
            _dbCricket.SubmitChanges();
        }
        public void UpdateBettingForMatch(Betting betting)
        {
            Betting updateObj = (from b in _dbCricket.Bettings
                                 where b.BettingTypeId.Equals(betting.BettingTypeId) && b.AdminId.Equals(betting.AdminId)
                                 && b.IsDeleted == false && b.LagadiRate.Equals(betting.LagadiRate) && b.KhadhiRate.Equals(betting.KhadhiRate)
                                 select b).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsDeleted = true;
                updateObj.UpdatedDate = System.DateTime.Now;
            }
            _dbCricket.SubmitChanges();
        }

        public void UpdateBettingForDraw(Betting betting)
        {
            Betting updateObj = (from b in _dbCricket.Bettings
                                 where b.BettingTypeId.Equals(betting.BettingTypeId) && b.AdminId.Equals(betting.AdminId) && b.TeamId.Equals(32)
                                 && b.IsDeleted == false && b.IsDraw == true
                                 select b).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsDeleted = true;
                updateObj.UpdatedDate = System.DateTime.Now;
            }
            _dbCricket.SubmitChanges();
        }
        public void UpdateBettingForTie(Betting betting)
        {
            Betting updateObj = (from b in _dbCricket.Bettings
                                 where b.BettingTypeId.Equals(betting.BettingTypeId) && b.AdminId.Equals(betting.AdminId) && b.TeamId.Equals(31)
                                 && b.IsDeleted == false && b.IsTie == true
                                 select b).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsDeleted = true;
            }
            _dbCricket.SubmitChanges();
        }
        //public void StopAllBetting(Betting betting)
        //{
        //    Betting updateObj = (from b in _dbCricket.Bettings
        //                         where b.AdminId.Equals(betting.AdminId) && b.IsDeleted == false 
        //                         select b).;
        //    if (updateObj != null)
        //    {
        //        updateObj.IsDeleted = true;
        //    }
        //    _dbCricket.SubmitChanges();
        //}
        public int StopAllBetting(int? AdminId)
        {

            var result = (from b in _dbCricket.Bettings where b.AdminId.Equals(AdminId) && b.IsDeleted.Equals(false) select b).ToList();
            if (result.Count > 0)
            {
                //for (int i = 0; i < result.Count; i++)
                //{
                //    result[i].IsDeleted = true;
                //    _dbCricket.SubmitChanges();
                //}


                result.ForEach(x => x.IsDeleted = true);
                _dbCricket.SubmitChanges();

                return 1;
            }
            else
                return 0;
        }

        public List<EditAdminBetResult> EditAdminBetting(int MatchId, int BetTypeId, int AdminId)
        {
            return _dbCricket.EditAdminBet(MatchId, BetTypeId, AdminId).ToList();
        }
        public List<Betting> GetBettingStatus(int BettingId)
        {
            var result = (from b in _dbCricket.Bettings where b.Id.Equals(BettingId) select b).ToList();
            return result;
        }
        #endregion

        #region Customer Betting
        public List<GetSessionBettingForClientResult> GetSessionBettingForClient(int adminId, int matchId, int bettingType)
        {
            var result = _dbCricket.GetSessionBettingForClient(adminId, matchId, bettingType).ToList();
            return result;
        }
        public void CreateCustomerBetting(List<CustomerBetting> customerBettings)
        {
            _dbCricket.CustomerBettings.InsertAllOnSubmit(customerBettings);
            _dbCricket.SubmitChanges();
        }

        public List<UpdateTeamARecordsResult> UpdateTeamARecords(int MatchId, int TeamId)
        {
            return _dbCricket.UpdateTeamARecords(MatchId, TeamId).ToList();
        }
        public List<UpdateTeamBRecordsResult> UpdateTeamBRecords(int MatchId, int TeamId)
        {
            return _dbCricket.UpdateTeamBRecords(MatchId, TeamId).ToList();
        }
        public void UpdateTeamA(int MatchId)
        {
            _dbCricket.UpdateTeamA(MatchId);
        }
        public void UpdateTeamB(int MatchId)
        {
            _dbCricket.UpdateTeamB(MatchId);
        }
        public void UpdateDraw(int MatchId)
        {
            _dbCricket.UpdateTeamDraw(MatchId);
        }
        public void UpdateTie(int MatchId)
        {
            _dbCricket.UpdateTeamTie(MatchId);
        }

        public List<ClientMatchLedgerResult> ClientMatchLedger(int AdminId, int MatchId, int ClientId)
        {
            var result = _dbCricket.ClientMatchLedger(AdminId, MatchId, ClientId).ToList();
            return result;
        }
        public List<ClientSessionLedgerResult> ClientSessionLedger(int AdminId, int MatchId, int ClientId)
        {
            var result = _dbCricket.ClientSessionLedger(AdminId, MatchId, ClientId).ToList();
            return result;
        }
        public List<ClientCommissionLedgerResult> ClientCommissionLedger(int MatchId, int ClientId)
        {
            var result = _dbCricket.ClientCommissionLedger(MatchId, ClientId).ToList();
            return result;
        }

        public List<AdminMatchLedgerResult> AdminMatchLedger(int AdminId, int MatchId)
        {
            var result = _dbCricket.AdminMatchLedger(AdminId, MatchId).ToList();
            return result;
        }
        public List<AdminSessionLedgerResult> AdminSessionLedger(int AdminId, int MatchId)
        {
            var result = _dbCricket.AdminSessionLedger(AdminId, MatchId).ToList();
            return result;
        }
        public List<AdminCommissionLedgerResult> AdminCommissionLedger(int AdminId, int MatchId)
        {
            var result = _dbCricket.AdminCommissionLedger(AdminId, MatchId).ToList();
            return result;
        }
        public List<IsDuplicateRecordResult> IsDuplicateRecord(int MatchScheduleId, int BettingId, string BetFor, int CreatedBy, string BettingAmount)
        {
            return _dbCricket.IsDuplicateRecord(MatchScheduleId, BettingId, BetFor, CreatedBy,BettingAmount).ToList();

        }

        #endregion

        #region Score
        public void CreateScore(Score score)
        {
            _dbCricket.Scores.InsertOnSubmit(score);
            _dbCricket.SubmitChanges();
        }
        public List<CheckMatchIdExistResult> CheckMatchIdExist(int MatchId, int TeamId)
        {
            return _dbCricket.CheckMatchIdExist(MatchId, TeamId).ToList();
        }
        public void UpdateScore(Score score)
        {
            Score updateObj = (from s in _dbCricket.Scores
                               where s.MatchScheduleId.Equals(score.MatchScheduleId)
                               select s).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.TeamId = score.TeamId;
                updateObj.Run = score.Run;
                updateObj.Over = score.Over;
            }
            _dbCricket.SubmitChanges();
        }
        public List<GetScoreByMatchIdResult> GetScoreByMatchId(int MatchId)
        {
            return _dbCricket.GetScoreByMatchId(MatchId).ToList();
        }
        #endregion

        #region Result
        public void CreateResult(Result result)
        {
            _dbCricket.Results.InsertOnSubmit(result);
            _dbCricket.SubmitChanges();
        }
        public List<GetOversByTeamIdAndMatchIdResult> GetOversByTeamAndMatch(int TeamId, int MatchId)
        {
            var result = _dbCricket.GetOversByTeamIdAndMatchId(TeamId, MatchId);
            return result.ToList();
        }
        public List<GetOversByMatchIdResult> GetOversByMatch(int MatchId)
        {
            var result = _dbCricket.GetOversByMatchId(MatchId);
            return result.ToList();
        }
        public void CalculateWinningResultsForCustomers(int matchScheduleId, int bettingTypeId, int matchTypeId, int over, int TeamId)
        {
            _dbCricket.CalculateWinningResultsForCustomers(matchScheduleId, bettingTypeId, matchTypeId, over, TeamId);
        }
        public void CalculateNineEleven(int matchScheduleId, int bettingTypeId, int matchTypeId, int over, int TeamId)
        {
            _dbCricket.CalculateForNineEleven(matchScheduleId, bettingTypeId, matchTypeId, over, TeamId);
        }
        #endregion

        #region Session Result
        public void DeleteAndCreate(MatchSessionResult matchSessionResult)
        {

            //var result = (from ms in _dbCricket.MatchSessionResults
            //              where ms.TeamId.Equals(matchSessionResult.TeamId) &&
            //              ms.MatchScheduleId.Equals(matchSessionResult.MatchScheduleId)
            //              select ms).ToList();

            //if (result != null)
            //    _dbCricket.MatchSessionResults.DeleteAllOnSubmit(result);

            _dbCricket.MatchSessionResults.InsertOnSubmit(matchSessionResult);
            _dbCricket.SubmitChanges();
        }
        public void Delete(MatchSessionResult matchSessionResult)
        {
            var result = (from ms in _dbCricket.MatchSessionResults
                          where ms.MatchScheduleId.Equals(matchSessionResult.MatchScheduleId)
                          select ms).ToList();
            if (result != null)
                _dbCricket.MatchSessionResults.DeleteAllOnSubmit(result);

            _dbCricket.SubmitChanges();
        }
        public List<MatchSessionResult> GetMatchSessionResultByTeam(int MatchId, int TeamId)
        {
            var result = (from msr in _dbCricket.MatchSessionResults
                          where msr.MatchScheduleId.Equals(MatchId) && msr.TeamId.Equals(TeamId)
                          select msr);
            return result.ToList();
        }
        public void UpdateMatchSessionResult(MatchSessionResult matchSessionResult)
        {
            MatchSessionResult updateObj = (from msr in _dbCricket.MatchSessionResults
                                            where msr.MatchScheduleId.Equals(matchSessionResult.MatchScheduleId) && msr.TeamId.Equals(matchSessionResult.TeamId) && msr.Over.Equals(matchSessionResult.Over)
                                            select msr).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.Runs = matchSessionResult.Runs;
            }
            _dbCricket.SubmitChanges();
        }
        #endregion

        #region TossWinnerTeam
        public void CreateTossWinner(TossWinnerTeam tossWinnerTeam)
        {
            _dbCricket.TossWinnerTeams.InsertOnSubmit(tossWinnerTeam);
            _dbCricket.SubmitChanges();
        }
        #endregion

        #region AdminToss
        public void CreateAdminToss(AdminToss adminToss)
        {
            _dbCricket.AdminTosses.InsertOnSubmit(adminToss);
            _dbCricket.SubmitChanges();
        }
        public List<AdminToss> checkMatchExistOrNot(int matchId)
        {
            var result = (from t in _dbCricket.AdminTosses
                          where t.MatchScheduleId.Equals(matchId)
                          select t);
            return result.ToList();
        }
        public void UpdateAdminToss(AdminToss adminToss)
        {
            AdminToss updateObj = (from a in _dbCricket.AdminTosses
                                   where a.MatchScheduleId.Equals(adminToss.MatchScheduleId)
                                   select a).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsOn = false;
            }
            _dbCricket.SubmitChanges();
        }
        public List<GetActiveMatchByIdResult> GetActiveMatchById(int MatchId)
        {
            return _dbCricket.GetActiveMatchById(MatchId).ToList();
        }
        #endregion

        #region TossBetting
        public void CreateTossBetting(TossBetting tossBetting)
        {
            _dbCricket.TossBettings.InsertOnSubmit(tossBetting);
            _dbCricket.SubmitChanges();
        }
        #endregion

        #region CommissionAmount
        public void CreateCommissionAmount(CommissionAmount commissionAmount)
        {
            _dbCricket.CommissionAmounts.InsertOnSubmit(commissionAmount);
            _dbCricket.SubmitChanges();
        }
        #endregion

        #region FancyNineEleven
        public void CreateFancyNineEleven(FancyNineEleven fancyNineEleven)
        {
            _dbCricket.FancyNineElevens.InsertOnSubmit(fancyNineEleven);
            _dbCricket.SubmitChanges();
        }
        public void UpdateFancyNineEleven(FancyNineEleven fancyNineEleven)
        {
            FancyNineEleven updateObj = (from fne in _dbCricket.FancyNineElevens
                                         where fne.BettingTypeId.Equals(fancyNineEleven.BettingTypeId) && fne.AdminId.Equals(fancyNineEleven.AdminId)
                                 && fne.IsDeleted == false
                                         select fne).FirstOrDefault();
            if (updateObj != null)
            {
                updateObj.IsDeleted = true;
                updateObj.UpdatedDate = System.DateTime.Now;
            }
            _dbCricket.SubmitChanges();
        }
        public List<GetFancyNineElevenResult> GetFancyNineEleven(int adminId, int matchId, int bettingType)
        {
            var result = _dbCricket.GetFancyNineEleven(adminId, matchId, bettingType).ToList();
            return result;
        }
        #endregion
    }
}
